/**
 * 
 */
package com.ubb.smartsearch.searchengineif.faroo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.ejb.Stateless;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.ubb.smartsearch.searchengineif.XMLResultParserLocal;
import com.ubb.smartsearch.searchengineif.entity.Response;
import com.ubb.smartsearch.searchengineif.entity.Result;

/**
 * @author Aron
 * 
 */
@Stateless
public class FarooXMLResultParser implements XMLResultParserLocal {

	public Response getResponseFromURL(String url)
			throws ParserConfigurationException, MalformedURLException,
			SAXException, IOException {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		// url = "api.xml";
		// Document doc = db.parse(url);
		Document doc = db.parse(new URL(url).openStream());

		return parseXMLResult(doc);
	}

	private Response parseXMLResult(Document document) {

		Response response = new Response();
		response.setResults(new ArrayList<Result>());

		document.getDocumentElement().normalize();

		Element root = (Element) document.getElementsByTagName("results").item(
				0);

		response.setQuery(getTagValue("query", root));
		response.setCount(Integer.parseInt(getTagValue("count", root)));
		response.setStart(Integer.parseInt(getTagValue("start", root)));
		response.setLength(Integer.parseInt(getTagValue("length", root)));
		response.setTime(Long.parseLong(getTagValue("time", root)));

		NodeList nList = root.getElementsByTagName("result");
		for (int temp = 0; temp < nList.getLength(); temp++) {

			Node nNode = nList.item(temp);
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {

				Element eElement = (Element) nNode;
				Result result = new Result();
				result.setTitle(getTagValue("title", eElement));
				result.setUrl(getTagValue("url", eElement));
				result.setDomain(getTagValue("domain", eElement));
				result.setIurl(getTagValue("imageUrl", eElement));
				result.setKwic(getTagValue("kwic", eElement));
				result.setAuthor(getTagValue("author", eElement));
				// result.setDate(getTagValue("firstPublished", eElement));
				// result.setNews(getTagValue("isNews", eElement));
				// result.setRelated(getTagValue("related", eElement));
				response.getResults().add(result);
			}
		}

		return response;
	}

	private static String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
				.getChildNodes();

		Node nValue = (Node) nlList.item(0);

		try {
			return nValue.getNodeValue();
		} catch (NullPointerException ex) {
			return null;
		}
	}
}
